package com.project.nimsen.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.stereotype.Repository;

import com.project.jdbc.dao.IBaseJdbcDao;
import com.project.jdbc.service.BaseServiceImpl;
import com.project.jdbc.utils.Finder;
import com.project.nimsen.dao.IBaseDao;
import com.project.nimsen.entity.SUnitsEntity;
import com.project.nimsen.service.ISUnitsService;
@Repository("sUnitsService")
public class SUnitsServiceImpl extends BaseServiceImpl implements
		ISUnitsService {
	@Resource
	IBaseDao baseDao;
	protected IBaseJdbcDao getBaseDao() {
		return baseDao;
	}
	public SUnitsEntity findByUserId(String userId) throws Exception {
		if(StringUtils.isBlank(userId))
			return null;
		Finder finder = new Finder("select * from S_UNITS where active=0 and id in (select UNIT_ID from S_USER_UNIT where active!=1 and USER_ID=:USER_ID)");
		finder.setParam("USER_ID", userId);
		List<SUnitsEntity> list = baseDao.queryForList(finder, SUnitsEntity.class);
		if(CollectionUtils.isEmpty(list))
			return null;
		return list.get(0);
	}
	public List<Map<String, Object>> findExitId(String id) throws Exception {
		if(StringUtils.isBlank(id))
			return null;
		Finder finder = new Finder("select * from S_UNITS where active=0");
		if(StringUtils.isNotBlank(id)){
			finder.append(" and ID!=:ID");
			finder.setParam("ID", id);
		}
		List<Map<String, Object>> list = baseDao.queryForList(finder);
		if(CollectionUtils.isEmpty(list))
			return null;
		return list;
	}
	public SUnitsEntity findById(String id) throws Exception {
		if(StringUtils.isBlank(id)){
			return null;
		}
		Finder finder = new Finder("select * from S_UNITS where ID=:ID");
		finder.setParam("ID", id);
		return baseDao.queryForObject(finder, SUnitsEntity.class);
	}

}
